﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Mvc;
using System.Web.Services;
using App.Codes;
using Common;
using Langben.BLL;
using Langben.DAL;
using Newtonsoft.Json;

namespace App.Controllers
{
    public class AndroidController : Controller
    {
        //
        // GET: /Android/
        [WebMethod]
        public FileContentResult GetImg(string personId)
        {
            SysPerson person = new SysPersonBLL().GetById(personId);
            FileStream buffer = new FileStream( Server.MapPath("../../up/")+   person.TXIMG, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(buffer);
            byte[] image = br.ReadBytes((int)buffer.Length);
            return base.File(image, "image/gif");
        }
        
        public JsonResult GetPersonMessage(string personId)
        {
            SysPerson person = new SysPersonBLL().GetById(personId);
            string danwei = new ClsCon().GetFieldValue("SELECT TOP 1 Name FROM dbo.SysDepartment WHERE Id='"+person.SysDepartmentId+"'");
            var varKeyPair1 = new { Name = person.MyName, DanWei = danwei, ZhiChen = person.Position, Tel = person.MobilePhoneNumber,Phone=person.PhoneNumber };

            return Json(varKeyPair1, JsonRequestBehavior.AllowGet);
        }

        public string Login(string name, string password)
        {
            name = name.Replace(" ", "");
            password = password.Replace(" ", "");
            string personid =
                new ClsCon().GetFieldValue("SELECT Id AS personcount FROM dbo.SysPerson WHERE Name='" + name +
                                           "' AND Password='" + EncryptAndDecrypte.EncryptString(password) + "'");
            if (!string.IsNullOrEmpty(personid))
            {

                return personid;
            }
            else
            {
                return "失败";
            }

        }

        public JsonResult GetOrder(string personId)
        {
            SysPerson person = new SysPersonBLL().GetById(personId);
            DataTable order = new ClsCon().CreatDaTable("SELECT TOP 1 BusinessGuid ,Type FROM dbo.[Order] WHERE CreatePerson='" + personId + "' AND ISNULL(IsWork,'0')='0'");
            if (order.Rows.Count > 0)
            {
                new ClsCon().udiDataBase("UPDATE dbo.[Order] SET IsWork='1' WHERE CreatePerson='" + personId + "'");
                var varKeyPair1 = new { BusinessGuid = order.Rows[0]["BusinessGuid"], Type = order.Rows[0]["Type"]};
                return Json(varKeyPair1, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("noorder", JsonRequestBehavior.AllowGet);
            }
            

            
        }

        public string PingJia(string PersonGuid,string BusinessGuid,string Content)
        {
            

            string sql =
                "INSERT INTO dbo.Appraise( RowGuid ,CreateTime ,CreatePerson ,PersonGuid ,BusinessGuid ,Content)VALUES  ( NEWID() ,GETDATE() ,'" +
                PersonGuid + "' ,'" + PersonGuid + "' ,'" + BusinessGuid + "' ,'" + Content + "')";
            bool issuccess =
                new ClsCon().Insert(sql
                   );

            if (issuccess)
            {
                return "success";
            }
            else
            {
                return "failed";
            }



        }

        public string GetBusiness(string personId)
        {
            DataTable dt = new ClsCon().CreatDaTable("SELECT RowGuid,BusinessName,describe,cailiao FROM dbo.View_PersonAndBusiness WHERE Id='" +
                                      personId + "'");
            return JsonConvert.SerializeObject(dt);
        }


        public string GetPoster()
        {
            string postercontent = new ClsCon().GetFieldValue("SELECT TOP 1 Content FROM dbo.Poster ORDER BY CreateTime DESC ");
            return postercontent;
        }
    }
}
